const queryUrl = "http://qt.gtimg.cn/q=";
var curPrice;
var name;
var yesterdayFin;
var todayStart;
var changeAmount;



setInterval("getPrice()",10000);

function getPrice() {
    const fullUrl = queryUrl + window.localStorage.market + window.localStorage.code;
    $.get(fullUrl,function(data){
        const info = data.split("~");
        let needAlert1 = true;
        let needAlert2 = true;
        name = info[1];
        yesterdayFin = info[4];
        todayStart = info[5];
        changeAmount = info[6];
		if(curPrice === info[3]) {
            return;
        }
        curPrice = info[3];
        if(window.localStorage.upPrice>0 && curPrice<window.localStorage.upPrice){
            needAlert1 = false;
        }
        if(window.localStorage.lowPrice>0 && curPrice>window.localStorage.lowPrice){
		    needAlert2 = false;
        }
        if(needAlert1 || needAlert2){
            notification();
        }else{
            console.log("query triggered but no alert!")
        }
    });
}

function notification(){
    const opt = {
        body: buildBody(),
        icon: "icon.png"
    };
    const noti = new Notification(curPrice, opt);

    function closeNoti() {
        noti.close();
    }
    setTimeout(closeNoti,4000);
}

function buildBody() {
    return name+"【"+ code +"】";
}

function buildInfo() {
    return "当前价："+curPrice + "<br/>" +
        "昨收："+yesterdayFin + "<br/>" +
        "今开："+todayStart + "<br/>" +
        "换手量："+changeAmount + "手<br/>";
}